package com.ph.irp.base.verifyproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialBalancedeadlineDAO;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialCustomerTypeDAO;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialAreaDAO;
import com.ph.irp.base.assistantmaterial.dao.PBaseAssistantmaterialIndustryDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCustomerDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectCustomerService;
import com.ph.irp.dto.CustomerDTO;
import com.ph.irp.model.PBaseAssistantmaterialArea;
import com.ph.irp.model.PBaseAssistantmaterialBalancedeadline;
import com.ph.irp.model.PBaseAssistantmaterialCustomercategory;
import com.ph.irp.model.PBaseAssistantmaterialIndustry;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectCustomer;
import com.ph.irp.model.PBaseVerifyprojectDepartment;

public class PBaseVerifyprojectCustomerServiceImpl implements PBaseVerifyprojectCustomerService {
	private PBaseVerifyprojectCustomerDao customerDao;
	private PBaseVerifyprojectDepartmentDao departmentDao;
	private PBaseVerifyprojectCandiateDao candiateDao;
	private PBaseAssistantmaterialIndustryDao industryDao;
	private PBaseAssistantmaterialAreaDAO areaDao;
	private PBaseAssistantmaterialBalancedeadlineDAO balanceDao;
	private PBaseAssistantmaterialCustomerTypeDAO  customerTypeDao;

	public PBaseAssistantmaterialCustomerTypeDAO getCustomerTypeDao() {
		return customerTypeDao;
	}

	public void setCustomerTypeDao(PBaseAssistantmaterialCustomerTypeDAO customerTypeDao) {
		this.customerTypeDao = customerTypeDao;
	}

	public PBaseVerifyprojectCustomerDao getCustomerDao() {
		return customerDao;
	}

	public void setCustomerDao(PBaseVerifyprojectCustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	public PBaseVerifyprojectDepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(PBaseVerifyprojectDepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public PBaseVerifyprojectCandiateDao getCandiateDao() {
		return candiateDao;
	}

	public void setCandiateDao(PBaseVerifyprojectCandiateDao candiateDao) {
		this.candiateDao = candiateDao;
	}

	public PBaseAssistantmaterialIndustryDao getIndustryDao() {
		return industryDao;
	}

	public void setIndustryDao(PBaseAssistantmaterialIndustryDao industryDao) {
		this.industryDao = industryDao;
	}

	public PBaseAssistantmaterialAreaDAO getAreaDao() {
		return areaDao;
	}

	public void setAreaDao(PBaseAssistantmaterialAreaDAO areaDao) {
		this.areaDao = areaDao;
	}

	public PBaseAssistantmaterialBalancedeadlineDAO getBalanceDao() {
		return balanceDao;
	}

	public void setBalanceDao(PBaseAssistantmaterialBalancedeadlineDAO balanceDao) {
		this.balanceDao = balanceDao;
	}

	@Override
	public void addCustomer(PBaseVerifyprojectCustomer customer)
			throws Throwable {
//		try {
//			if (customerDao.isSame(customer.getCustomerCode())) {
//				throw new PHException("代码【" + customer.getCustomerCode()+ "】已经存在");
//			} else {
//				customerDao.addCustomer(customer);
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//			throw new PHException("在添加客户的时候出现异常，请重试或联系管理员");
//		}
		customerDao.addCustomer(customer);
	}

	@Override
	public void deleteCustomer(int customerId) throws Throwable {
//		try {
//			customerDao.deleteCustomer(customerId);
//		} catch (SQLException e) {
//			e.printStackTrace();
//			throw new PHException("在删除客户的时候出现异常，该客户可能在其他地方被引用到,请确认后重试");
//		}
		customerDao.deleteCustomer(customerId);
	}

	@Override
	public CustomerDTO getCustomerById(int customerId) throws Throwable {
			CustomerDTO c = new CustomerDTO();
			PBaseVerifyprojectCustomer customer = customerDao
					.getCustomerById(customerId);

			int PBaseVerifyprojectDepartmentId = customer.getPBaseVerifyprojectDepartmentId();
			int PBaseVerifyprojectCandiateId = customer.getPBaseVerifyprojectCandiateId();
			int PBaseAssistantmaterialIndustryId = customer.getPBaseAssistantmaterialIndustryId();
			int PBaseAssistantmaterialBalancedeadlineId = customer.getPBaseAssistantmaterialBalancedeadlineId();
			int PBaseAssistantmaterialCustomercategoryId = customer.getPBaseAssistantmaterialCustomercategoryId();
			int PBaseAssistantmaterialAreaId = customer.getPBaseAssistantmaterialAreaId();
			int customerTypeId=customer.getPBaseAssistantmaterialCustomercategoryId();
			int balancedeadlineId=customer.getPBaseAssistantmaterialBalancedeadlineId();

			c.setCustomer(customer);
			if (PBaseVerifyprojectDepartmentId != 0) {
				PBaseVerifyprojectDepartment department = departmentDao
						.getDepartmentById(PBaseVerifyprojectDepartmentId);
				c.setDepartment(department);
			}
			if (PBaseVerifyprojectCandiateId != 0) {
				PBaseVerifyprojectCandiate candiate = candiateDao
						.getCandiateById(PBaseVerifyprojectCandiateId);
				c.setCandiate(candiate);
			}
			if (PBaseAssistantmaterialIndustryId != 0) {
				PBaseAssistantmaterialIndustry industry = industryDao
						.getPBaseAssistantmaterialIndustryById(PBaseAssistantmaterialIndustryId);
				c.setIndustry(industry);
			}
			if (PBaseAssistantmaterialBalancedeadlineId != 0) {
				PBaseAssistantmaterialBalancedeadline balancedeadline = balanceDao.getBalancedeadlineById(balancedeadlineId);
				c.setBalancedeadline(balancedeadline);
				c.setBalancedeadline(balancedeadline);
			}
			if (PBaseAssistantmaterialCustomercategoryId != 0) {
				PBaseAssistantmaterialCustomercategory customerCategory =customerTypeDao.getCustomerTypeById(customerTypeId);
				c.setCustomerCategory(customerCategory);
			}
			if (PBaseAssistantmaterialAreaId != 0) {
				PBaseAssistantmaterialArea area = areaDao
						.getAssistantmaterialAreaById(PBaseAssistantmaterialAreaId);
				c.setArea(area);
			}
			return c;
	}

	@Override
	public List<CustomerDTO> getCustomerList(PBaseVerifyprojectCustomer customer)
			throws Throwable {
		List<CustomerDTO> customers = new ArrayList<CustomerDTO>();
			List<PBaseVerifyprojectCustomer> list = customerDao.getCustomerList(customer);
			for (PBaseVerifyprojectCustomer pbvc : list) {
				CustomerDTO c = new CustomerDTO();
				int PBaseVerifyprojectDepartmentId = pbvc.getPBaseVerifyprojectDepartmentId();
				int PBaseVerifyprojectCandiateId = pbvc.getPBaseVerifyprojectCandiateId();
				int PBaseAssistantmaterialIndustryId = pbvc.getPBaseAssistantmaterialIndustryId();
				int PBaseAssistantmaterialBalancedeadlineId = pbvc.getPBaseAssistantmaterialBalancedeadlineId();
				int PBaseAssistantmaterialCustomercategoryId = pbvc.getPBaseAssistantmaterialCustomercategoryId();
				int PBaseAssistantmaterialAreaId = pbvc.getPBaseAssistantmaterialAreaId();
				int balancedeadlineId=pbvc.getPBaseAssistantmaterialBalancedeadlineId();
				int customerTypeId=pbvc.getPBaseAssistantmaterialCustomercategoryId();
				
				c.setCustomer(pbvc);
				if (PBaseVerifyprojectDepartmentId != 0) {
					PBaseVerifyprojectDepartment department = departmentDao
							.getDepartmentById(PBaseVerifyprojectDepartmentId);
					c.setDepartment(department);
				}
				if (PBaseVerifyprojectCandiateId != 0) {
					PBaseVerifyprojectCandiate candiate = candiateDao
							.getCandiateById(PBaseVerifyprojectCandiateId);
					c.setCandiate(candiate);
				}
				if (PBaseAssistantmaterialIndustryId != 0) {
					PBaseAssistantmaterialIndustry industry = industryDao
							.getPBaseAssistantmaterialIndustryById(PBaseAssistantmaterialIndustryId);
					c.setIndustry(industry);
				}
				if (PBaseAssistantmaterialBalancedeadlineId != 0) {
					PBaseAssistantmaterialBalancedeadline balancedeadline = balanceDao.getBalancedeadlineById(balancedeadlineId);
					c.setBalancedeadline(balancedeadline);
				}
				if (PBaseAssistantmaterialCustomercategoryId != 0) {
					PBaseAssistantmaterialCustomercategory customerCategory =customerTypeDao.getCustomerTypeById(customerTypeId);
					c.setCustomerCategory(customerCategory);
				}
				if (PBaseAssistantmaterialAreaId != 0) {
					PBaseAssistantmaterialArea area = areaDao.getAssistantmaterialAreaById(PBaseAssistantmaterialAreaId);
					c.setArea(area);
				}
				customers.add(c);
			}
		return customers;
	}

	@Override
	public void updateCustomer(PBaseVerifyprojectCustomer customer)
			throws Throwable {
//		PBaseVerifyprojectCustomer pbvc;
//		try {
//			pbvc = customerDao.getCustomerById(customer.getCustomerId());
//			if (customer.getCustomerCode().equals(pbvc.getCustomerCode())) {
//				customerDao.updateCustomer(customer);
//			} else {
//				if (customerDao.isSame(customer.getCustomerCode())) {
//					throw new PHException("代码【" + customer.getCustomerCode()+ "】已经存在");
//				} else {
//					customerDao.updateCustomer(customer);
//				}
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//			throw new PHException("在更新客户信息的时候出现异常，请重试或联系管理员");
//		}
		customerDao.updateCustomer(customer);
	}

}
